using UnityEngine;
using System.Collections.Generic;

public class FlockManager : MonoBehaviour
{
    [Header("队列设置")]
    public GameObject birdPrefab;        // 鸟的预制体
    public int rows = 5;                // 行数(m)
    public int columns = 5;             // 列数(n)
    public float spacing = 2f;          // 鸟之间的间距
    public float altitude = 5f;         // 生成平面的高度(Y轴)

    [Header("随机化设置")]
    public bool addRandomness = true;   // 是否添加随机位置偏移
    public float randomness = 0.3f;     // 随机位置偏移量

    [Header("方向设置")]
    public Vector3 initialDirection = Vector3.forward; // 初始朝向
    public bool alignWithFormation = true; // 是否对齐队形方向

    void Start()
    {
        GenerateFormation();
    }

    void GenerateFormation()
    {
        // 计算队形中心点
        Vector3 formationCenter = new Vector3(
            transform.position.x,
            transform.position.y + altitude,
            transform.position.z
        );

        // 计算总宽度和长度
        float totalWidth = (columns - 1) * spacing;
        float totalLength = (rows - 1) * spacing;

        // 生成起始位置(左上角)
        Vector3 startPos = formationCenter - new Vector3(totalWidth / 2, 0, totalLength / 2);

        for (int row = 0; row < rows; row++)
        {
            for (int col = 0; col < columns; col++)
            {
                // 计算基础位置
                Vector3 basePos = startPos + new Vector3(
                    col * spacing,
                    0,
                    row * spacing
                );

                // 添加随机偏移
                Vector3 finalPos = basePos;
                if (addRandomness)
                {
                    finalPos += new Vector3(
                        Random.Range(-randomness, randomness),
                        Random.Range(-randomness, randomness),
                        Random.Range(-randomness, randomness)
                    );
                }

                // 实例化鸟
                GameObject bird = Instantiate(birdPrefab, finalPos, Quaternion.identity);
                bird.transform.parent = transform;

                // 设置初始朝向
                if (alignWithFormation)
                {
                    bird.transform.rotation = Quaternion.LookRotation(initialDirection);
                }

                // 可选: 为每只鸟添加唯一标识
                bird.name = $"Bird_{row}_{col}";
            }
        }

        Debug.Log($"成功生成 {rows}×{columns} 的鸟群队列，共 {rows * columns} 只鸟");
    }

    // 在场景视图中绘制生成范围(辅助可视化)
    void OnDrawGizmosSelected()
    {
        Gizmos.color = Color.cyan;

        // 计算队形边界
        float totalWidth = (columns - 1) * spacing;
        float totalLength = (rows - 1) * spacing;

        Vector3 center = new Vector3(
            transform.position.x,
            transform.position.y + altitude,
            transform.position.z
        );

        Vector3 topLeft = center - new Vector3(totalWidth / 2, 0, totalLength / 2);
        Vector3 topRight = topLeft + new Vector3(totalWidth, 0, 0);
        Vector3 bottomLeft = topLeft + new Vector3(0, 0, totalLength);
        Vector3 bottomRight = topRight + new Vector3(0, 0, totalLength);

        // 绘制矩形边界
        Gizmos.DrawLine(topLeft, topRight);
        Gizmos.DrawLine(topRight, bottomRight);
        Gizmos.DrawLine(bottomRight, bottomLeft);
        Gizmos.DrawLine(bottomLeft, topLeft);

        // 绘制朝向指示
        Gizmos.color = Color.yellow;
        Gizmos.DrawRay(center, initialDirection * 2f);
    }
}